package fun.boomcat.luckyhe.mirai.plugin.botcontrol.util;

import fun.boomcat.luckyhe.mirai.plugin.botcontrol.pojo.Config;
import net.mamoe.mirai.console.command.CommandSender;

import java.io.IOException;
import java.util.List;

/**
 * @author Lucky_He
 * @date 2021年8月29日 8:28
 * @description MessageEventGroupOperation
 */
public class MessageEventGroupOperation {
    public static void megOperation(CommandSender commandSender, Object[] args, String primaryName) {
//        对消息事件群的操作
        int len = args.length;

        String operation = args[0].toString();
        long groupId;

        switch (operation.toLowerCase()) {
            case "add":
            case "添加":

                try {
                    groupId = Long.parseLong(args[1].toString());
                } catch (Exception e) {
                    commandSender.sendMessage("错误：" + e.toString());
                    return;
                }

                megAdd(commandSender, groupId);
                break;

            case "del":
            case "删除":

                try {
                    groupId = Long.parseLong(args[1].toString());
                } catch (Exception e) {
                    commandSender.sendMessage("错误：" + e.toString());
                    return;
                }

                megDel(commandSender, groupId);
                break;

            default:
                commandSender.sendMessage(ControlCommandHelp.messageEventGroupHelp(primaryName));
                break;
        }
    }

    public static void megAdd(CommandSender commandSender, long groupId) {
//        添加一个消息通知群
        Config config;
        try {
            config = ConfigUtil.getConfig();
        } catch (IOException e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        List<Long> groupsForGroupMessageEvent = config.getGroupsForGroupMessageEvent();
        if (groupsForGroupMessageEvent.contains(groupId)) {
            commandSender.sendMessage("已存在条目" + groupId);
        } else {
            groupsForGroupMessageEvent.add(groupId);

            try {
                ConfigUtil.writeConfig();
            } catch (IOException e) {
                commandSender.sendMessage("错误：" + e.toString());
            }

            commandSender.sendMessage("已添加条目" + groupId);
        }
    }

    public static void megDel(CommandSender commandSender, long groupId) {
//        删除一个消息通知群
        Config config;
        try {
            config = ConfigUtil.getConfig();
        } catch (IOException e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        List<Long> groupsForGroupMessageEvent = config.getGroupsForGroupMessageEvent();
        if (!groupsForGroupMessageEvent.contains(groupId)) {
            commandSender.sendMessage("不存在条目" + groupId);
        } else {
            groupsForGroupMessageEvent.remove(groupId);

            try {
                ConfigUtil.writeConfig();
            } catch (IOException e) {
                commandSender.sendMessage("错误：" + e.toString());
            }

            commandSender.sendMessage("已删除条目" + groupId);
        }
    }
}
